/*--------------------------------------------------------------
 * File: sntp_task.c 编码类型：C语言  UTF-8
 * Author WRS (1352509846@qq.com)
 * 功能: NTP网络校时功能
 * Version 0.1
 * Date 2024-05-07
 *
 * @copyright Copyright (c) 2024
 *
----------------------------------------------------------------*/
#include "Dev_task.h"
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_task_wdt.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_sleep.h"
#include "nvs_flash.h"
#include "driver/uart.h"
#include "system/inc/global.h"
#include "my_uart.h"

extern const Env_ST Env_st;
extern uint32_t _gSysClock;

TaskHandle_t Dev_task_Handle = NULL;

static const char *TAG = "Dev_task";

static void Dev_task_cb(void *pvParameter)
{
    uint16_t length = 0;
    const uint16_t max_iterations = 500;  // 连续执行次数
    
    while (1)
    {        
        uart_get_buffered_data_len(Env_st.MasterPort, &length);
        if(length > 0)
        {
            for(uint16_t i = 0; i < max_iterations; i++)
            {
                DisposeMasterPortReceive(Env_st.MasterPort);
            }
        }
        _gSysClock++;
        vTaskDelay(1); // Check every 10ms        
    }
}

void Dev_task_init(void)
{
    ESP_LOGI(TAG, "Create Dev_task");
    uart_Init();
    xTaskCreate(&Dev_task_cb, "Dev_task_cb", 8192, NULL, 8, &Dev_task_Handle);
}


void Dev_task_Deinit(void)
{
    ESP_LOGI(TAG, "Delete Dev_task");
    if (Dev_task_Handle != NULL) 
    {
        vTaskDelete(Dev_task_Handle);
        Dev_task_Handle = NULL;  // 任务删除后设置句柄为NULL
        uart_DeInit();
    }
}

